package edu.xja.controller;

import edu.xja.entity.BankAccount;
import edu.xja.service.BankAccountService;
import edu.xja.service.impl.BankAccountServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/bankaccount")
public class BankAccountController extends HttpServlet {

    private BankAccountService bankAccountService=new BankAccountServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name=request.getParameter("name");
        String balance=request.getParameter("balance");
        BankAccount bankAccount=new BankAccount();
        bankAccount.setName(name);
        bankAccount.setBalance(Integer.parseInt(balance));
        int result=bankAccountService.add(bankAccount);
        if(result==1){
            request.setAttribute("tip_info","添加账户成功");
            request.getRequestDispatcher("/admin/success.jsp").forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id=request.getParameter("id");
        BankAccount bankAccount=bankAccountService.getById(Integer.valueOf(id));
        request.setAttribute("bank_account",bankAccount);
        request.getRequestDispatcher("/admin/bankaccount.jsp").forward(request,response);
    }
}
